import {ref, computed} from 'vue'
import {defineStore} from 'pinia'
import {successCode} from "~/api/base/http.js";
import {getOrderList, getShoppingList} from "~/api/orderApi.js";
import {getLoginUser} from "~/api/userApi.js";
import {ROLE_ADMIN, ROLE_MASTER, ROLE_NORMAL, ROLE_ROOT, ROLE_USER} from "~/utils/const.js";

export const userInfoStore = defineStore('userInfo', {
    state: () => {
        return {
            shoppingList: [],//购物车
            doubleCount: computed(() => 2 * 2),
            loginUser: null,
            powerName: false,
            powerNumber: 0,
            root: false,
            master: false,
            admin: false,
            normal: false,
            user: false,
            authorization: "",
            powerTable: [
                {label: "游客", value: ROLE_USER},
                {label: "普通用户", value: ROLE_NORMAL},
                {label: "管理员", value: ROLE_ADMIN},
                {label: "站长", value: ROLE_MASTER},
                {label: "ROOT", value: ROLE_ROOT},
            ],
        }
    },
    actions: {
        checkUserLogin() {
            return this.user;
        },
        addShopping(info) {
            this.shoppingList.push(info);
        },
        initShopping() {
            getShoppingList(1, 99999999).then(res => {
                if (res.code === successCode) {
                    this.shoppingList = res.data.content;
                }
            })
        },
        isUser(powerNum) {
            return this.powerTable[0].value === powerNum
        },
        getPowerTableOption() {
            return [
                {type: "info", data: this.powerTable[0].label, msg: this.powerTable[0].label},
                {type: "warring", data: this.powerTable[1].label, msg: this.powerTable[1].label},
                {type: "primary", data: this.powerTable[2].label, msg: this.powerTable[2].label},
                {type: "success", data: this.powerTable[3].label, msg: this.powerTable[3].label},
                {type: "danger", data: this.powerTable[4].label, msg: this.powerTable[4].label},
            ]
        },
        logout() {
            this.loginUser = null;
            this.powerName = false;
            this.powerNumber = 0;
            this.root = false;
            this.master = false;
            this.admin = false;
            this.normal = false;
            this.user = false;
        },
        initLogUser(isFresh = false) {
            if (isFresh || this.loginUser === null) {
                getLoginUser().then((res) => {
                    if (successCode === res.code) {
                        this.setLoginUser(res.data)
                    }
                })
            }
        },
        setLoginUser(userFromServer) {
            if (userFromServer) {
                this.loginUser = userFromServer;
                this.powerName = userFromServer.powerName;
                this.powerNumber = userFromServer.powerNumber;
                const power = this.loginUser.powerNumber;
                this.user = true
                if (power >= this.powerTable[1].value) {
                    this.normal = true;
                }
                if (power >= this.powerTable[2].value) {
                    this.admin = true;
                }
                if (power >= this.powerTable[3].value) {
                    this.master = true;
                }
                if (power >= this.powerTable[4].value) {
                    this.root = true;
                }
                this.loginUser.powerNumber = null;
                this.loginUser.password = null;
            }
        },
        delLoginUser() {
            this.loginUser = ref({})
            this.powerName = ref(false);
            this.powerNumber = ref(1);
            this.root = ref(false);
            this.master = ref(false);
            this.admin = ref(false);
            this.normal = ref(false);
            this.user = ref(false);
            this.authorization = ref("");
        }
    }
})
